package enduringLink.call;

import data.handle.Result;
import enduringLink.MyProxy;
import enduringLink.RequestHandler;
import io.netty.channel.Channel;
import local.LocalInstance;

import java.util.LinkedList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;

public class MyTransfer implements Transfer {

    private LinkedBlockingQueue<Result> results = new LinkedBlockingQueue<>();
    private MyQuantityHandle myQuantityHandle;
    private CountDownLatch countDownLatch;

    public MyTransfer(MyQuantityHandle myQuantityHandle){
        this.myQuantityHandle = myQuantityHandle;
    }

    @Override
    public void get(byte brandIndex) {
        Channel channel = RequestHandler.getChannel();
        MyProxy myProxy = new MyProxy(channel);
        Transfer transfer = myProxy.proxyGet(Transfer.class);
        double avgQuantity = myQuantityHandle.getTotalQuantity() / 2000000;
        LinkedBlockingQueue<Result> results = LocalInstance.getResult(brandIndex, avgQuantity * 0.3);
        System.out.println("brandIndex : " + brandIndex + " results :" + results);
        transfer.transferData(results);
    }

    @Override
    public void transferData(LinkedBlockingQueue<Result> res) {

        for (Result re : res) {
            results.add(re);
        }
        countDownLatch.countDown();

    }

    public void setCountDownLatch(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    public LinkedBlockingQueue<Result> getResults() {
        return results;
    }

}
